﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace Marmat.Forms
{
    [ToolboxItem(false)]
    [ToolboxBitmap(typeof(System.Windows.Forms.DataGrid))]
    public partial class BaseGrid : System.Windows.Forms.UserControl, IBindingFieldNotificationBus, ISupportInitialize, IControl
    {
        public BaseGrid()
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.BaseGridView = this.gridView1;
            this.BaseBandedGridView = this.bandedGridView1;
            this.BaseAdvBandedGridView = this.advBandedGridView1;
            this.GridControl1.DataSource = this.BindingSource;
            this.BaseAdvBandedGridView.OptionsView.ShowGroupPanel = true;
            this.BaseAdvBandedGridView.OptionsView.ShowColumnHeaders = true;
            this.VisualStyle = VisualStyleEnum.GridView;
        }

        private GridView _BaseGridView;

        protected internal GridView BaseGridView
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get => _BaseGridView;
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (_BaseGridView != null)
                {
                    /// <summary>
                    /// 绘制行号及行状态的颜色
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    /// <remarks></remarks>
                    _BaseGridView.CustomDrawRowIndicator -= BaseGridView_CustomDrawRowIndicator;

                    /// <summary>
                    /// 鼠标按下之后触发
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    /// <remarks></remarks>
                    _BaseGridView.MouseDown -= GridView1_MouseDown;
                    _BaseGridView.CellMerge -= GridView1_CellMerge;

                    /// <summary>
                    /// 绘制单元格
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    /// <remarks></remarks>
                    _BaseGridView.CustomDrawCell -= BaseGridView_CustomDrawCell;

                    _BaseGridView.ValidatingEditor -= GridView1_ValidatingEditor;
                    /// <summary>
                    /// 准备编辑单元格的时候执行
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    /// <remarks></remarks>
                    _BaseGridView.ShowingEditor -= BaseGridView_ShowingEditor;
                    _BaseGridView.ShownEditor -= GridView1_ShownEditor;
                    _BaseGridView.KeyDown -= GridView1_KeyDown;
                    _BaseGridView.KeyPress -= GridView1_KeyPress;
                    _BaseGridView.KeyUp -= GridView1_KeyUp;
                    _BaseGridView.CellValueChanging -= GridView1_CellValueChanging;
                    _BaseGridView.CellValueChanged -= GridView1_CellValueChanged;
                    _BaseGridView.CustomRowCellEditForEditing -= GridView1_CustomRowCellEditForEditing;
                    _BaseGridView.CustomRowCellEdit -= GridView1_CustomRowCellEdit;
                    _BaseGridView.HiddenEditor -= BaseGridView_HiddenEditor;
                    _BaseGridView.CalcRowHeight -= GridView1_CalcRowHeight;

                    _BaseGridView.GotFocus -= GridView1_GotFocus;
                    _BaseGridView.LostFocus -= GridView1_LostFocus;
                    _BaseGridView.CustomColumnGroup -= GridView1_CustomColumnGroup;
                    _BaseGridView.CustomColumnSort -= GridView1_CustomColumnSort;
                    _BaseGridView.EndGrouping -= GridView1_EndGrouping;

                    /// <summary>
                    /// 排序操作完成后触发。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.EndSorting -= GridView1_EndSorting;

                    /// <summary>
                    /// 在开始分组操作之前触发。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.StartGrouping -= GridView1_StartGrouping;

                    /// <summary>
                    /// 在排序操作开始之前触发。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.StartSorting -= GridView1_StartSorting;
                    _BaseGridView.ColumnWidthChanged -= GridView1_ColumnWidthChanged;
                    _BaseGridView.FocusedColumnChanged -= GridView1_FocusedColumnChanged;
                    _BaseGridView.FocusedRowChanged -= GridView1_FocusedRowChanged;
                    _BaseGridView.SelectionChanged -= GridView1_SelectionChanged;
                    _BaseGridView.TopRowChanged -= GridView1_TopRowChanged;
                    _BaseGridView.MouseEnter -= GridView1_MouseEnter;
                    _BaseGridView.MouseLeave -= GridView1_MouseLeave;

                    /// <summary>
                    /// 鼠标移动时触发
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    /// <remarks></remarks>
                    _BaseGridView.MouseMove -= GridView1_MouseMove;
                    _BaseGridView.MouseUp -= GridView1_MouseUp;
                    _BaseGridView.MouseWheel -= GridView1_MouseWheel;
                    _BaseGridView.CustomColumnDisplayText -= GridView1_CustomColumnDisplayText;


                    /// <summary>
                    /// 在显示此单元格之前，为每个网格单元格触发。允许您修改外观。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.RowCellStyle -= GridView1_RowCellStyle;

                    /// <summary>
                    /// 允许您修改行的样式。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.RowStyle -= GridView1_RowStyle;

                    /// <summary>
                    /// 绘制列标题时发生。
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    _BaseGridView.CustomDrawColumnHeader -= GridView1_CustomDrawColumnHeader;
                    _BaseGridView.BeforeLeaveRow -= GridView1_BeforeLeaveRow;
                    _BaseGridView.Click -= GridView1_Click;
                    _BaseGridView.DoubleClick -= GridView1_DoubleClick;
                    _BaseGridView.InvalidRowException -= GridView1_InvalidRowException;
                    _BaseGridView.InvalidValueException -= GridView1_InvalidValueException;
                    _BaseGridView.RowCellClick -= GridView1_RowCellClick;
                    _BaseGridView.RowClick -= GridView1_RowClick;
                    _BaseGridView.ValidateRow -= GridView1_ValidateRow;
                    _BaseGridView.RowCountChanged -= BaseGridView_RowCountChanged;
                }

                _BaseGridView = value;
                if (_BaseGridView != null)
                {
                    _BaseGridView.CustomDrawRowIndicator += BaseGridView_CustomDrawRowIndicator;
                    _BaseGridView.MouseDown += GridView1_MouseDown;
                    _BaseGridView.CellMerge += GridView1_CellMerge;
                    _BaseGridView.CustomDrawCell += BaseGridView_CustomDrawCell;
                    _BaseGridView.ValidatingEditor += GridView1_ValidatingEditor;
                    _BaseGridView.ShowingEditor += BaseGridView_ShowingEditor;
                    _BaseGridView.ShownEditor += GridView1_ShownEditor;
                    _BaseGridView.KeyDown += GridView1_KeyDown;
                    _BaseGridView.KeyPress += GridView1_KeyPress;
                    _BaseGridView.KeyUp += GridView1_KeyUp;
                    _BaseGridView.CellValueChanging += GridView1_CellValueChanging;
                    _BaseGridView.CellValueChanged += GridView1_CellValueChanged;
                    _BaseGridView.CustomRowCellEditForEditing += GridView1_CustomRowCellEditForEditing;
                    _BaseGridView.CustomRowCellEdit += GridView1_CustomRowCellEdit;
                    _BaseGridView.HiddenEditor += BaseGridView_HiddenEditor;
                    _BaseGridView.CalcRowHeight += GridView1_CalcRowHeight;
                    _BaseGridView.GotFocus += GridView1_GotFocus;
                    _BaseGridView.LostFocus += GridView1_LostFocus;
                    _BaseGridView.CustomColumnGroup += GridView1_CustomColumnGroup;
                    _BaseGridView.CustomColumnSort += GridView1_CustomColumnSort;
                    _BaseGridView.EndGrouping += GridView1_EndGrouping;
                    _BaseGridView.EndSorting += GridView1_EndSorting;
                    _BaseGridView.StartGrouping += GridView1_StartGrouping;
                    _BaseGridView.StartSorting += GridView1_StartSorting;
                    _BaseGridView.ColumnWidthChanged += GridView1_ColumnWidthChanged;
                    _BaseGridView.FocusedColumnChanged += GridView1_FocusedColumnChanged;
                    _BaseGridView.FocusedRowChanged += GridView1_FocusedRowChanged;
                    _BaseGridView.SelectionChanged += GridView1_SelectionChanged;
                    _BaseGridView.TopRowChanged += GridView1_TopRowChanged;
                    _BaseGridView.MouseEnter += GridView1_MouseEnter;
                    _BaseGridView.MouseLeave += GridView1_MouseLeave;
                    _BaseGridView.MouseMove += GridView1_MouseMove;
                    _BaseGridView.MouseUp += GridView1_MouseUp;
                    _BaseGridView.MouseWheel += GridView1_MouseWheel;
                    _BaseGridView.CustomColumnDisplayText += GridView1_CustomColumnDisplayText;
                    _BaseGridView.RowCellStyle += GridView1_RowCellStyle;
                    _BaseGridView.RowStyle += GridView1_RowStyle;
                    _BaseGridView.CustomDrawColumnHeader += GridView1_CustomDrawColumnHeader;
                    _BaseGridView.BeforeLeaveRow += GridView1_BeforeLeaveRow;
                    _BaseGridView.Click += GridView1_Click;
                    _BaseGridView.DoubleClick += GridView1_DoubleClick;
                    _BaseGridView.InvalidRowException += GridView1_InvalidRowException;
                    _BaseGridView.InvalidValueException += GridView1_InvalidValueException;
                    _BaseGridView.RowCellClick += GridView1_RowCellClick;
                    _BaseGridView.RowClick += GridView1_RowClick;
                    _BaseGridView.ValidateRow += GridView1_ValidateRow;
                    _BaseGridView.RowCountChanged += BaseGridView_RowCountChanged;
                }
            }
        }

        private DevExpress.XtraGrid.Views.BandedGrid.BandedGridView _BaseBandedGridView;

        protected internal DevExpress.XtraGrid.Views.BandedGrid.BandedGridView BaseBandedGridView
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get => _BaseBandedGridView;
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (_BaseBandedGridView != null)
                {
                    _BaseBandedGridView.MouseDown -= GridView1_MouseDown;
                    _BaseBandedGridView.CustomDrawCell -= BaseGridView_CustomDrawCell;
                    _BaseBandedGridView.KeyPress -= GridView1_KeyPress;
                    _BaseBandedGridView.KeyUp -= GridView1_KeyUp;
                    _BaseBandedGridView.GotFocus -= GridView1_GotFocus;
                    _BaseBandedGridView.LostFocus -= GridView1_LostFocus;
                    _BaseBandedGridView.CustomColumnGroup -= GridView1_CustomColumnGroup;
                    _BaseBandedGridView.CustomColumnSort -= GridView1_CustomColumnSort;
                    _BaseBandedGridView.EndGrouping -= GridView1_EndGrouping;
                    _BaseBandedGridView.EndSorting -= GridView1_EndSorting;
                    _BaseBandedGridView.StartGrouping -= GridView1_StartGrouping;
                    _BaseBandedGridView.StartSorting -= GridView1_StartSorting;
                    _BaseBandedGridView.ColumnWidthChanged -= GridView1_ColumnWidthChanged;
                    _BaseBandedGridView.FocusedColumnChanged -= GridView1_FocusedColumnChanged;
                    _BaseBandedGridView.FocusedRowChanged -= GridView1_FocusedRowChanged;
                    _BaseBandedGridView.SelectionChanged -= GridView1_SelectionChanged;
                    _BaseBandedGridView.TopRowChanged -= GridView1_TopRowChanged;
                    _BaseBandedGridView.MouseEnter -= GridView1_MouseEnter;
                    _BaseBandedGridView.MouseLeave -= GridView1_MouseLeave;
                    _BaseBandedGridView.MouseUp -= GridView1_MouseUp;
                    _BaseBandedGridView.MouseWheel -= GridView1_MouseWheel;
                    _BaseBandedGridView.CustomColumnDisplayText -= GridView1_CustomColumnDisplayText;
                    _BaseBandedGridView.RowCellStyle -= GridView1_RowCellStyle;
                    _BaseBandedGridView.RowStyle -= GridView1_RowStyle;
                    _BaseBandedGridView.CustomDrawColumnHeader -= GridView1_CustomDrawColumnHeader;
                    _BaseBandedGridView.BeforeLeaveRow -= GridView1_BeforeLeaveRow;
                    _BaseBandedGridView.Click -= GridView1_Click;
                    _BaseBandedGridView.DoubleClick -= GridView1_DoubleClick;
                    _BaseBandedGridView.InvalidRowException -= GridView1_InvalidRowException;
                    _BaseBandedGridView.InvalidValueException -= GridView1_InvalidValueException;
                    _BaseBandedGridView.RowCellClick -= GridView1_RowCellClick;
                    _BaseBandedGridView.RowClick -= GridView1_RowClick;
                    _BaseBandedGridView.ValidateRow -= GridView1_ValidateRow;
                }

                _BaseBandedGridView = value;
                if (_BaseBandedGridView != null)
                {
                    _BaseBandedGridView.MouseDown += GridView1_MouseDown;
                    _BaseBandedGridView.CustomDrawCell += BaseGridView_CustomDrawCell;
                    _BaseBandedGridView.KeyPress += GridView1_KeyPress;
                    _BaseBandedGridView.KeyUp += GridView1_KeyUp;
                    _BaseBandedGridView.GotFocus += GridView1_GotFocus;
                    _BaseBandedGridView.LostFocus += GridView1_LostFocus;
                    _BaseBandedGridView.CustomColumnGroup += GridView1_CustomColumnGroup;
                    _BaseBandedGridView.CustomColumnSort += GridView1_CustomColumnSort;
                    _BaseBandedGridView.EndGrouping += GridView1_EndGrouping;
                    _BaseBandedGridView.EndSorting += GridView1_EndSorting;
                    _BaseBandedGridView.StartGrouping += GridView1_StartGrouping;
                    _BaseBandedGridView.StartSorting += GridView1_StartSorting;
                    _BaseBandedGridView.ColumnWidthChanged += GridView1_ColumnWidthChanged;
                    _BaseBandedGridView.FocusedColumnChanged += GridView1_FocusedColumnChanged;
                    _BaseBandedGridView.FocusedRowChanged += GridView1_FocusedRowChanged;
                    _BaseBandedGridView.SelectionChanged += GridView1_SelectionChanged;
                    _BaseBandedGridView.TopRowChanged += GridView1_TopRowChanged;
                    _BaseBandedGridView.MouseEnter += GridView1_MouseEnter;
                    _BaseBandedGridView.MouseLeave += GridView1_MouseLeave;
                    _BaseBandedGridView.MouseUp += GridView1_MouseUp;
                    _BaseBandedGridView.MouseWheel += GridView1_MouseWheel;
                    _BaseBandedGridView.CustomColumnDisplayText += GridView1_CustomColumnDisplayText;
                    _BaseBandedGridView.RowCellStyle += GridView1_RowCellStyle;
                    _BaseBandedGridView.RowStyle += GridView1_RowStyle;
                    _BaseBandedGridView.CustomDrawColumnHeader += GridView1_CustomDrawColumnHeader;
                    _BaseBandedGridView.BeforeLeaveRow += GridView1_BeforeLeaveRow;
                    _BaseBandedGridView.Click += GridView1_Click;
                    _BaseBandedGridView.DoubleClick += GridView1_DoubleClick;
                    _BaseBandedGridView.InvalidRowException += GridView1_InvalidRowException;
                    _BaseBandedGridView.InvalidValueException += GridView1_InvalidValueException;
                    _BaseBandedGridView.RowCellClick += GridView1_RowCellClick;
                    _BaseBandedGridView.RowClick += GridView1_RowClick;
                    _BaseBandedGridView.ValidateRow += GridView1_ValidateRow;
                }
            }
        }

        private DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView _BaseAdvBandedGridView;

        protected internal DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView BaseAdvBandedGridView
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get => _BaseAdvBandedGridView;
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (_BaseAdvBandedGridView != null)
                {
                    _BaseAdvBandedGridView.CustomDrawRowIndicator -= BaseGridView_CustomDrawRowIndicator;
                    _BaseAdvBandedGridView.MouseDown -= GridView1_MouseDown;
                    _BaseAdvBandedGridView.CellMerge -= GridView1_CellMerge;
                    _BaseAdvBandedGridView.CustomDrawCell -= BaseGridView_CustomDrawCell;
                    _BaseAdvBandedGridView.ValidatingEditor -= GridView1_ValidatingEditor;
                    _BaseAdvBandedGridView.ShowingEditor -= BaseGridView_ShowingEditor;
                    _BaseAdvBandedGridView.ShownEditor -= GridView1_ShownEditor;
                    _BaseAdvBandedGridView.KeyDown -= GridView1_KeyDown;
                    _BaseAdvBandedGridView.CellValueChanging -= GridView1_CellValueChanging;
                    _BaseAdvBandedGridView.CellValueChanged -= GridView1_CellValueChanged;
                    _BaseAdvBandedGridView.CustomRowCellEditForEditing -= GridView1_CustomRowCellEditForEditing;
                    _BaseAdvBandedGridView.CustomRowCellEdit -= GridView1_CustomRowCellEdit;
                    _BaseAdvBandedGridView.CalcRowHeight -= GridView1_CalcRowHeight;
                    _BaseAdvBandedGridView.GotFocus -= GridView1_GotFocus;
                    _BaseAdvBandedGridView.LostFocus -= GridView1_LostFocus;
                    _BaseAdvBandedGridView.CustomColumnGroup -= GridView1_CustomColumnGroup;
                    _BaseAdvBandedGridView.CustomColumnSort -= GridView1_CustomColumnSort;
                    _BaseAdvBandedGridView.EndGrouping -= GridView1_EndGrouping;
                    _BaseAdvBandedGridView.EndSorting -= GridView1_EndSorting;
                    _BaseAdvBandedGridView.StartGrouping -= GridView1_StartGrouping;
                    _BaseAdvBandedGridView.StartSorting -= GridView1_StartSorting;
                    _BaseAdvBandedGridView.ColumnWidthChanged -= GridView1_ColumnWidthChanged;
                    _BaseAdvBandedGridView.FocusedColumnChanged -= GridView1_FocusedColumnChanged;
                    _BaseAdvBandedGridView.FocusedRowChanged -= GridView1_FocusedRowChanged;
                    _BaseAdvBandedGridView.SelectionChanged -= GridView1_SelectionChanged;
                    _BaseAdvBandedGridView.TopRowChanged -= GridView1_TopRowChanged;
                    _BaseAdvBandedGridView.MouseEnter -= GridView1_MouseEnter;
                    _BaseAdvBandedGridView.MouseLeave -= GridView1_MouseLeave;
                    _BaseAdvBandedGridView.MouseMove -= GridView1_MouseMove;
                    _BaseAdvBandedGridView.MouseUp -= GridView1_MouseUp;
                    _BaseAdvBandedGridView.MouseWheel -= GridView1_MouseWheel;
                    _BaseAdvBandedGridView.CustomColumnDisplayText -= GridView1_CustomColumnDisplayText;
                    _BaseAdvBandedGridView.RowCellStyle -= GridView1_RowCellStyle;
                    _BaseAdvBandedGridView.RowStyle -= GridView1_RowStyle;
                    _BaseAdvBandedGridView.CustomDrawColumnHeader -= GridView1_CustomDrawColumnHeader;
                    _BaseAdvBandedGridView.BeforeLeaveRow -= GridView1_BeforeLeaveRow;
                    _BaseAdvBandedGridView.Click -= GridView1_Click;
                    _BaseAdvBandedGridView.DoubleClick -= GridView1_DoubleClick;
                    _BaseAdvBandedGridView.InvalidRowException -= GridView1_InvalidRowException;
                    _BaseAdvBandedGridView.InvalidValueException -= GridView1_InvalidValueException;
                    _BaseAdvBandedGridView.RowCellClick -= GridView1_RowCellClick;
                    _BaseAdvBandedGridView.RowClick -= GridView1_RowClick;
                    _BaseAdvBandedGridView.ValidateRow -= GridView1_ValidateRow;
                }

                _BaseAdvBandedGridView = value;
                if (_BaseAdvBandedGridView != null)
                {
                    _BaseAdvBandedGridView.CustomDrawRowIndicator += BaseGridView_CustomDrawRowIndicator;
                    _BaseAdvBandedGridView.MouseDown += GridView1_MouseDown;
                    _BaseAdvBandedGridView.CellMerge += GridView1_CellMerge;
                    _BaseAdvBandedGridView.CustomDrawCell += BaseGridView_CustomDrawCell;
                    _BaseAdvBandedGridView.ValidatingEditor += GridView1_ValidatingEditor;
                    _BaseAdvBandedGridView.ShowingEditor += BaseGridView_ShowingEditor;
                    _BaseAdvBandedGridView.ShownEditor += GridView1_ShownEditor;
                    _BaseAdvBandedGridView.KeyDown += GridView1_KeyDown;
                    _BaseAdvBandedGridView.CellValueChanging += GridView1_CellValueChanging;
                    _BaseAdvBandedGridView.CellValueChanged += GridView1_CellValueChanged;
                    _BaseAdvBandedGridView.CustomRowCellEditForEditing += GridView1_CustomRowCellEditForEditing;
                    _BaseAdvBandedGridView.CustomRowCellEdit += GridView1_CustomRowCellEdit;
                    _BaseAdvBandedGridView.CalcRowHeight += GridView1_CalcRowHeight;
                    _BaseAdvBandedGridView.GotFocus += GridView1_GotFocus;
                    _BaseAdvBandedGridView.LostFocus += GridView1_LostFocus;
                    _BaseAdvBandedGridView.CustomColumnGroup += GridView1_CustomColumnGroup;
                    _BaseAdvBandedGridView.CustomColumnSort += GridView1_CustomColumnSort;
                    _BaseAdvBandedGridView.EndGrouping += GridView1_EndGrouping;
                    _BaseAdvBandedGridView.EndSorting += GridView1_EndSorting;
                    _BaseAdvBandedGridView.StartGrouping += GridView1_StartGrouping;
                    _BaseAdvBandedGridView.StartSorting += GridView1_StartSorting;
                    _BaseAdvBandedGridView.ColumnWidthChanged += GridView1_ColumnWidthChanged;
                    _BaseAdvBandedGridView.FocusedColumnChanged += GridView1_FocusedColumnChanged;
                    _BaseAdvBandedGridView.FocusedRowChanged += GridView1_FocusedRowChanged;
                    _BaseAdvBandedGridView.SelectionChanged += GridView1_SelectionChanged;
                    _BaseAdvBandedGridView.TopRowChanged += GridView1_TopRowChanged;
                    _BaseAdvBandedGridView.MouseEnter += GridView1_MouseEnter;
                    _BaseAdvBandedGridView.MouseLeave += GridView1_MouseLeave;
                    _BaseAdvBandedGridView.MouseMove += GridView1_MouseMove;
                    _BaseAdvBandedGridView.MouseUp += GridView1_MouseUp;
                    _BaseAdvBandedGridView.MouseWheel += GridView1_MouseWheel;
                    _BaseAdvBandedGridView.CustomColumnDisplayText += GridView1_CustomColumnDisplayText;
                    _BaseAdvBandedGridView.RowCellStyle += GridView1_RowCellStyle;
                    _BaseAdvBandedGridView.RowStyle += GridView1_RowStyle;
                    _BaseAdvBandedGridView.CustomDrawColumnHeader += GridView1_CustomDrawColumnHeader;
                    _BaseAdvBandedGridView.BeforeLeaveRow += GridView1_BeforeLeaveRow;
                    _BaseAdvBandedGridView.Click += GridView1_Click;
                    _BaseAdvBandedGridView.DoubleClick += GridView1_DoubleClick;
                    _BaseAdvBandedGridView.InvalidRowException += GridView1_InvalidRowException;
                    _BaseAdvBandedGridView.InvalidValueException += GridView1_InvalidValueException;
                    _BaseAdvBandedGridView.RowCellClick += GridView1_RowCellClick;
                    _BaseAdvBandedGridView.RowClick += GridView1_RowClick;
                    _BaseAdvBandedGridView.ValidateRow += GridView1_ValidateRow;
                }
            }
        }


        /// <summary>
        /// 是否将列设为自动宽度
        /// </summary>
        /// <remarks></remarks>
        [DefaultValue(false)]
        [Category("外观")]
        [Description("是否将列设为自动宽度。")]
        public bool ColumnAutoWidth
        {
            get
            {
                return this._ColumnAutoWidth;
            }

            set
            {
                if (this._ColumnAutoWidth != value)
                {
                    this._ColumnAutoWidth = value;
                    this.SetValue("OptionsView.ColumnAutoWidth", value);
                }
            }
        }

        private bool _ColumnAutoWidth = false;

        /// <summary>
        /// 新建行的标题文本。
        /// </summary>
        /// <returns></returns>
        [Category("外观")]
        [Description("新建行的标题文本")]
        [DefaultValue("新增")]
        public string NewItemRowText
        {
            get
            {
                return this.m_NewItemRowText;
            }

            set
            {
                this.m_NewItemRowText = value;
                if (this.BaseGridView is object)
                {
                    this.BaseGridView.NewItemRowText = this.m_NewItemRowText;
                }

                if (this.BaseAdvBandedGridView is object)
                {
                    this.BaseAdvBandedGridView.NewItemRowText = this.m_NewItemRowText;
                }
            }
        }

        private string m_NewItemRowText = "新增";

        private void BaseGridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                DataRow row = null;
                if (ReferenceEquals(sender, this.BaseGridView))
                {
                    row = this.BaseGridView.GetDataRow(e.RowHandle);
                }
                else if (ReferenceEquals(sender, this.BaseAdvBandedGridView))
                {
                    row = this.BaseAdvBandedGridView.GetDataRow(e.RowHandle);
                }
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                if (row is not null)
                {
                    var Color = System.Drawing.Color.SteelBlue;
                    switch (row.RowState)
                    {
                        case DataRowState.Added: // 新增                            
                            Color = System.Drawing.Color.Red;
                            break;
                        case DataRowState.Modified: // 修改                            
                            Color = System.Drawing.Color.Orange;
                            break;
                    }
                    e.Info.BackAppearance.ForeColor = Color;
                }
            }
            else if (!this.Editable)
            {
                e.DefaultDraw();
                var icon = Properties.Resources.Lock;
                e.Graphics.DrawIcon(icon, (int)Math.Round(e.Bounds.X + (e.Bounds.Width - icon.Width) / 2d), (int)Math.Round(e.Bounds.Y + (e.Bounds.Height - icon.Height) / 2d));
            }
        }

        private void GridView1_MouseDown(object sender, MouseEventArgs e)
        {
            MouseDown?.Invoke(this, e);
            GridHitInfo info = (GridHitInfo)this.BaseControl.MainView.CalcHitInfo(e.X, e.Y);
            switch (e.Button)
            {
                case MouseButtons.Left:
                    if (info.InColumnPanel && ModifierKeys == Keys.Control)
                    {
                        if (string.Equals(this.Sort, info.Column.FieldName))
                            this.Sort = info.Column.FieldName + " desc";
                        else
                            this.Sort = info.Column.FieldName;
                    }
                    else if (this.ContainsNewItemRow(info, e.Location) == true) // 如果点击的是新增操作列的话
                    {
                        CellNewRowLink?.Invoke(this, EventArgs.Empty);
                    }
                    else if (e.Clicks == 2 && info.InRow) // 如果是双击的话
                    {
                        if (info.Column?.Tag is Views.Grid.Column dc)
                        {
                            var row = this.BaseControl.MainView.GetRow(info.RowHandle);
                            var EventArgs = new Views.Grid.MouseRowColEventArgs(this, info.RowHandle, dc, e.Location, e.Button);
                            if (dc is object && row is object)
                            {
                                RowCellDoubleClick?.Invoke(this, EventArgs);
                            }
                        }
                    }
                    else if (info.Column is object)
                    {
                        if (info.Column.Tag is object && info.Column.Tag is Views.Grid.Column dc)
                        {
                            switch (dc.ColumnEdit)
                            {
                                case ColumnEditEnum.Link:
                                    if (this.ContainsLink(info, e.Location)) // 如果点击超链接的话
                                    {
                                        var row = this.BaseControl.MainView.GetRow(info.RowHandle);
                                        var mouseRowColEventArgs = new Views.Grid.MouseRowColEventArgs(this, info.RowHandle, dc, e.Location, e.Button);
                                        CellLinkClick?.Invoke(this, mouseRowColEventArgs);
                                    }
                                    break;
                            }
                        }
                    }
                    break;
                case MouseButtons.Right:
                    var eventArgs = new Views.Grid.BeforeShowContextMenuEventArgs(this);
                    if (this.BaseGridView.IsCellSelected(info.RowHandle, info.Column))
                        eventArgs._HitType = Views.Grid.HitTypeEnum.Range;
                    else if (info.HitTest == GridHitTest.RowCell)
                        eventArgs._HitType = Views.Grid.HitTypeEnum.Cell;
                    else if (info.HitTest == GridHitTest.RowIndicator)
                        eventArgs._HitType = Views.Grid.HitTypeEnum.Row;
                    else if (info.HitTest == GridHitTest.Column)
                        eventArgs._HitType = Views.Grid.HitTypeEnum.Column;
                    else if (info.HitTest == GridHitTest.EmptyRow)
                        eventArgs._HitType = Views.Grid.HitTypeEnum.Empty;
                    else
                        return;
                    if (info.RowHandle >= 0)
                        this.BindingSource.Position = info.RowHandle;
                    BeforeShowContextMenu?.Invoke(this, eventArgs);
                    if (eventArgs.ContextMenu is not null && eventArgs.Cancel == false)
                    {
                        eventArgs.ContextMenu.Show(MousePosition);
                    }
                    break;
            }
        }

        /// <summary>
        /// 检查鼠标当前位置是否是新增行的操作列
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pt"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool ContainsNewItemRow(GridHitInfo info, Point pt)
        {
            bool IsNewItemRow = false;
            GridViewInfo ViewInfo = null;
            if (this.BaseGridView is object)
            {
                ViewInfo = (GridViewInfo)this.BaseGridView.GetViewInfo();
                IsNewItemRow = this.BaseGridView.IsNewItemRow(info.RowHandle);
            }
            else if (this.BaseAdvBandedGridView is object)
            {
                ViewInfo = (GridViewInfo)this.BaseAdvBandedGridView.GetViewInfo();
                IsNewItemRow = this.BaseAdvBandedGridView.IsNewItemRow(info.RowHandle);
            }

            if (IsNewItemRow && info.Column is object && info.Column.Visible && info.Column.VisibleIndex == 0)
            {
                var GridCellInfo = ViewInfo.GetGridCellInfo(info);
                var size = TextRenderer.MeasureText(this.NewItemRowText, this.link_Font);
                var erect = new Rectangle(GridCellInfo.Bounds.X, (int)Math.Round(GridCellInfo.Bounds.Y + (GridCellInfo.Bounds.Height - size.Height) / 2d), size.Width, size.Height);
                if (erect.Contains(pt))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 检查当前位置是链接列的操作列
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pt"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private bool ContainsLink(GridHitInfo info, Point pt)
        {
            GridViewInfo viewInfo = null;
            string displayText = null;
            if (this.BaseGridView is object)
            {
                viewInfo = (GridViewInfo)this.BaseGridView.GetViewInfo();
                displayText = this.BaseGridView.GetRowCellDisplayText(info.RowHandle, info.Column);
            }
            else if (this.BaseAdvBandedGridView is object)
            {
                viewInfo = (GridViewInfo)this.BaseAdvBandedGridView.GetViewInfo();
                displayText = this.BaseAdvBandedGridView.GetRowCellDisplayText(info.RowHandle, info.Column);
            }

            var gridCellInfo = viewInfo.GetGridCellInfo(info);
            if (gridCellInfo is object && !string.IsNullOrEmpty(displayText))
            {
                var bounds = new Rectangle();
                var size = TextRenderer.MeasureText(displayText, this.link_Font);
                switch (info.Column.AppearanceCell.HAlignment)
                {
                    case DevExpress.Utils.HorzAlignment.Default:
                    case DevExpress.Utils.HorzAlignment.Near:
                        bounds = new Rectangle(gridCellInfo.Bounds.X, (int)Math.Round(gridCellInfo.Bounds.Y + (gridCellInfo.Bounds.Height - size.Height) / 2d), size.Width, size.Height);
                        break;
                    case DevExpress.Utils.HorzAlignment.Center:

                        bounds = new Rectangle((int)Math.Round(gridCellInfo.Bounds.X + (gridCellInfo.Bounds.Width - size.Width) / 2d), (int)Math.Round(gridCellInfo.Bounds.Y + (gridCellInfo.Bounds.Height - size.Height) / 2d), size.Width, size.Height);
                        break;
                    case DevExpress.Utils.HorzAlignment.Far:
                        bounds = new Rectangle(gridCellInfo.Bounds.X + (gridCellInfo.Bounds.Width - size.Width), (int)Math.Round(gridCellInfo.Bounds.Y + (gridCellInfo.Bounds.Height - size.Height) / 2d), size.Width, size.Height);
                        break;
                }
                return bounds.Contains(pt);
            }

            return false;
        }

        /// <summary>
        /// 获取或设置表格组件是否允许编辑。
        /// </summary>
        /// <returns></returns>
        [DefaultValue(true)]
        public virtual bool Editable
        {
            get => this.m_Editable;
            set
            {
                this.m_Editable = value;
                this.NotifyPropertyChanged();
                if (this.BaseGridView is not null)
                    this.BaseGridView.OptionsBehavior.Editable = this.Editable;
                else if (this.BaseAdvBandedGridView is not null)
                    this.BaseAdvBandedGridView.OptionsBehavior.Editable = this.Editable;
            }
        }

        private bool m_Editable = true;

        private void GridView1_CellMerge(object sender, CellMergeEventArgs e)
        {
            var info = new Views.Grid.CellMergeEventArgs(this, e);
            CellMerge?.Invoke(this, info);
        }

        private void BaseGridView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column is null || e.Column.Tag is null)
                return;

            if (e.RowHandle >= 0)
            {
                if (ReferenceEquals(e.CellValue, DBNull.Value))
                    e.CellValue = null;
                var info = new Views.Grid.RowCellCustomDrawEventArgs(this, e);
                // 处理高精度小数多余的0的问题。
                if (e.CellValue is object && e.Column.ColumnType == typeof(decimal))
                {
                    if (string.IsNullOrEmpty(e.DisplayText) && string.IsNullOrEmpty(e.Column.DisplayFormat.FormatString) && info.Column.Decimals > 0)
                    {
                        var fomat = $"{{0:0.{new string('0', info.Column.Decimals)}}}";
                        info.DisplayText = string.Format(fomat, info.CellValue);
                    }
                }

                CustomDrawCell?.Invoke(this, info);
            }

            bool is_newRow = false;
            // 更新字体信息==================================================
            if (this.link_Font is null || !string.Equals(this.link_Font.Name, this.Font.Name))
            {
                this.link_Font = new Font(this.Font, FontStyle.Underline);
            }

            // 判断绘制的行是否属于新增行==================================================
            switch (this.VisualStyle)
            {
                case VisualStyleEnum.GridView:
                    is_newRow = this.BaseGridView.IsNewItemRow(e.RowHandle) && this.BaseGridView.OptionsView.NewItemRowPosition == NewItemRowPosition.Bottom && e.Column.Visible == true;
                    break;
                case VisualStyleEnum.BandedGridView:
                    is_newRow = this.BaseBandedGridView.IsNewItemRow(e.RowHandle) && this.BaseBandedGridView.OptionsView.NewItemRowPosition == NewItemRowPosition.Bottom && e.Column.Visible == true;
                    break;
                case VisualStyleEnum.AdvBandedGridView:
                    is_newRow = this.BaseAdvBandedGridView.IsNewItemRow(e.RowHandle) && this.BaseAdvBandedGridView.OptionsView.NewItemRowPosition == NewItemRowPosition.Bottom && e.Column.Visible == true;
                    break;
            }

            // 如果当前单元格是新增行的话==================================================
            if (is_newRow)
            {
                if (e.Column.VisibleIndex == 0)
                {
                    e.DisplayText = this.NewItemRowText;
                    e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
                    e.Appearance.Font = this.link_Font;
                    e.Appearance.ForeColor = Color.Blue;
                }
                else
                {
                    e.DisplayText = null;
                }

                return;
            }
            // 如果当前列属于标准列的话==================================================
            if (e.Column.Tag is Views.Grid.Column column)
            {
                switch (column.ColumnEdit)
                {
                    case ColumnEditEnum.Link:
                        e.Appearance.Font = this.link_Font;
                        e.Appearance.ForeColor = Color.Blue;
                        break;
                }
            }
        }

        private Font link_Font;

        private void GridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            DevExpress.XtraGrid.Columns.GridColumn column = null;
            var rowHandle = default(int);
            object Row = null;
            if (ReferenceEquals(sender, this.BaseGridView))
            {
                column = this.BaseGridView.FocusedColumn;
                rowHandle = this.BaseGridView.FocusedRowHandle;
                Row = this.BaseGridView.GetRow(this.BaseGridView.FocusedRowHandle);
            }
            else if (ReferenceEquals(sender, this.BaseBandedGridView))
            {
                column = this.BaseBandedGridView.FocusedColumn;
                rowHandle = this.BaseBandedGridView.FocusedRowHandle;
                Row = this.BaseBandedGridView.GetRow(this.BaseBandedGridView.FocusedRowHandle);
            }
            else if (ReferenceEquals(sender, this.BaseAdvBandedGridView))
            {
                column = this.BaseAdvBandedGridView.FocusedColumn;
                rowHandle = this.BaseAdvBandedGridView.FocusedRowHandle;
                Row = this.BaseAdvBandedGridView.GetRow(this.BaseAdvBandedGridView.FocusedRowHandle);
            }

            // 触发相应事件==================================================
            if (column is object && column.Tag is Views.Grid.Column)
            {
                var EventArgs = new Views.Grid.ValidateEditEventArgs(this, rowHandle, (Views.Grid.Column)column.Tag, e);
                ValidatingEditor?.Invoke(this, EventArgs);
            }
        }

        private void BaseGridView_ShowingEditor(object sender, CancelEventArgs e)
        {
            DevExpress.XtraGrid.Columns.GridColumn column = null;
            bool IsNewRow = false;
            var rowHandle = default(int);
            if (ReferenceEquals(sender, this.BaseGridView))
            {
                column = this.BaseGridView.FocusedColumn;
                rowHandle = this.BaseGridView.FocusedRowHandle;
                IsNewRow = this.BaseGridView.IsNewItemRow(this.BaseGridView.FocusedRowHandle);
            }
            else if (ReferenceEquals(sender, this.BaseAdvBandedGridView))
            {
                column = this.BaseAdvBandedGridView.FocusedColumn;
                rowHandle = this.BaseAdvBandedGridView.FocusedRowHandle;
                IsNewRow = this.BaseAdvBandedGridView.IsNewItemRow(this.BaseAdvBandedGridView.FocusedRowHandle);
            }
            if (column.Tag is object && column.Tag is Views.Grid.Column dc)
            {
                if (IsNewRow || dc.ColumnEdit == ColumnEditEnum.Link)
                {
                    e.Cancel = true;
                }

                if (e.Cancel == false)
                {
                    // 触发相应事件==================================================
                    var EventArgs = new Views.Grid.RowColCancelEventArgs(this, rowHandle, dc, e);
                    ShowingEditor?.Invoke(this, EventArgs);
                    e.Cancel = EventArgs.Cancel;
                }
            }
        }

        private void GridView1_ShownEditor(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Columns.GridColumn Column = null;
            object Row = null;
            var rowHandle = default(int);
            if (ReferenceEquals(sender, this.BaseGridView))
            {
                Column = this.BaseGridView.FocusedColumn;
                rowHandle = this.BaseGridView.FocusedRowHandle;
                Row = this.BaseGridView.GetRow(rowHandle);
            }
            else if (ReferenceEquals(sender, this.BaseAdvBandedGridView))
            {
                Column = this.BaseAdvBandedGridView.FocusedColumn;
                rowHandle = this.BaseAdvBandedGridView.FocusedRowHandle;
                Row = this.BaseAdvBandedGridView.GetRow(rowHandle);
            }

            // 触发相应事件==================================================
            if (Column is object && Column.Tag is Views.Grid.Column)
            {
                var EventArgs = new Views.Grid.RowColEventArgs(this, rowHandle, (Views.Grid.Column)Column.Tag);
                ShownEditor?.Invoke(this, EventArgs);
            }
        }

        private void GridView1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Delete:
                case Keys.Control | Keys.X:
                    if (!this.Editable) break;
                    if (this.VisualStyle is VisualStyleEnum.GridView or VisualStyleEnum.AdvBandedGridView)
                    {
                        var mainView = this.GetMainView();
                        var cells = mainView.GetSelectedCells();
                        foreach (DevExpress.XtraGrid.Views.Base.GridCell cell in cells)
                        {
                            Views.Grid.Column column = (Views.Grid.Column)cell.Column.Tag;
                            var row = this.BaseGridView.GetRow(cell.RowHandle);
                            if (column?.AllowEdit == true)
                            {
                                var EventArgs = new Views.Grid.RowColCancelEventArgs(this, cell.RowHandle, column);
                                ShowingEditor?.Invoke(this, EventArgs);
                                if (EventArgs.Cancel == false)
                                {
                                    object value = null;
                                    if (ReferenceEquals(cell.Column.ColumnType, typeof(bool)))
                                        value = false;
                                    var validateEditEventArgs = new Views.Grid.ValidateEditEventArgs(this, cell.RowHandle, column, new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs(value));
                                    ValidatingEditor?.Invoke(this, validateEditEventArgs);
                                    if (validateEditEventArgs.Cancel == false)
                                    {
                                        mainView.SetRowCellValue(cell.RowHandle, cell.Column, value);
                                    }
                                }
                            }
                        }
                        // 屏蔽按键信息==================================================
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    break;
                case Keys.Enter:
                    SendKeys.Send("{tab}");
                    break;
                case Keys.Control | Keys.C:
                    if (this.AllowClipBoard == false)
                        e.Handled = true;
                    break;
                case Keys.Control | Keys.D:
                    {
                        if (this.Editable == false) break; // 如果当前表不允许编辑的话，直接结束
                        int rowHandle = this.GetMainView().FocusedRowHandle;
                        var Column = this.BaseGridView.FocusedColumn;
                        if (rowHandle >= 0 && Column?.Tag is Views.Grid.Column dc)
                        {
                            int Col_StartIndex = Column.VisibleIndex;
                            int RowsCount = this.BaseGridView.SelectedRowsCount;
                            if (dc is object && dc.AllowEdit)
                            {
                                var value = this.GetRowCellValue(rowHandle, dc);
                                for (int i = 0, loopTo = RowsCount - 1; i <= loopTo; i++)
                                {
                                    int RowIndex = rowHandle + i;
                                    var row = this.BaseGridView.GetRow(RowIndex);
                                    if (this.BaseGridView.RowCount > RowIndex)
                                    {
                                        var EventArgs = new Views.Grid.RowColCancelEventArgs(this, RowIndex, dc);
                                        ShowingEditor?.Invoke(this, EventArgs);
                                        if (EventArgs.Cancel == false)
                                        {
                                            var validateEditEventArgs = new Views.Grid.ValidateEditEventArgs(this, RowIndex, dc, new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs(value));
                                            ValidatingEditor?.Invoke(this, validateEditEventArgs);
                                            if (validateEditEventArgs.Cancel == false)
                                            {
                                                this.BaseGridView.SetRowCellValue(RowIndex, Column, value);
                                            }
                                        }
                                    }
                                }
                                // 屏蔽按键信息==================================================
                                e.Handled = true;
                                e.SuppressKeyPress = true;
                            }
                        }
                    }
                    break;
                case Keys.Control | Keys.V: // 粘贴
                    {
                        if (this.Editable == false) break; // 如果当前表不允许编辑的话，直接结束

                        // 第一步，先对剪切板的数据进行分割==================================================
                        string GetText = Clipboard.GetText().Trim();
                        var txt = GetText.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        var RowCollection = new List<List<string>>();
                        // 分行==================================================
                        foreach (string line in txt)
                        {
                            var ColCollection = new List<string>();
                            string lineStr = line.Trim();
                            if (!string.IsNullOrEmpty(lineStr))
                            {
                                // 分列==================================================
                                foreach (string value in lineStr.Split())
                                    ColCollection.Add(value);
                            }

                            RowCollection.Add(ColCollection);
                        }
                        // 数据准备完成，准备拷贝至表格当中==================================================
                        if (this.BaseGridView.FocusedRowHandle >= 0 && this.BaseGridView.FocusedColumn is object)
                        {
                            int Row_StartIndex = this.BaseGridView.FocusedRowHandle;
                            int Col_StartIndex = this.BaseGridView.FocusedColumn.VisibleIndex;
                            for (int Row_Index = 0, loopTo1 = RowCollection.Count - 1; Row_Index <= loopTo1; Row_Index++)
                            {
                                int RowIndex = Row_StartIndex + Row_Index;
                                var row = this.BaseGridView.GetRow(RowIndex);
                                // 如果行数足够的话，逐行赋值(通过列进行赋值)==================================================
                                if (this.BaseGridView.RowCount > RowIndex)
                                {
                                    var ColCollection = RowCollection[Row_Index];
                                    for (int Col_Index = 0, loopTo2 = ColCollection.Count - 1; Col_Index <= loopTo2; Col_Index++)
                                    {
                                        int ColIndex = Col_StartIndex + Col_Index;
                                        // 如果列数足够的话，逐行赋值==================================================
                                        if (this.BaseGridView.VisibleColumns.Count > ColIndex)
                                        {
                                            var Column = this.BaseGridView.VisibleColumns[ColIndex];
                                            Views.Grid.Column dc = (Views.Grid.Column)Column.Tag;
                                            if (dc is object && dc.AllowEdit)
                                            {
                                                var EventArgs = new Views.Grid.RowColCancelEventArgs(this, RowIndex, dc);
                                                ShowingEditor?.Invoke(this, EventArgs);
                                                if (EventArgs.Cancel == false)
                                                {
                                                    object value = ColCollection[Col_Index];
                                                    var validateEditEventArgs = new Views.Grid.ValidateEditEventArgs(this, RowIndex, dc, new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs(value));
                                                    ValidatingEditor?.Invoke(this, validateEditEventArgs);
                                                    if (validateEditEventArgs.Cancel == false)
                                                    {
                                                        this.BaseGridView.SetRowCellValue(RowIndex, Column, value);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        // 粘贴完成，屏蔽按键信息==================================================
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                        break;
                    }
            }
            KeyDown?.Invoke(this, e);
        }

        private void GridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            KeyPress?.Invoke(this, e);
        }

        private void GridView1_KeyUp(object sender, KeyEventArgs e)
        {
            KeyUp?.Invoke(this, e);
        }

        private void GridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Tag is Views.Grid.Column column)
            {
                var eventArgs = new Views.Grid.CellValueChangingEventArgs(this, e.RowHandle, column, e.Value);
                CellValueChanging?.Invoke(this, eventArgs);
            }
        }

        private void GridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            bool IsNewItemRow = false;
            switch (this.VisualStyle)
            {
                case VisualStyleEnum.GridView:
                    IsNewItemRow = this.BaseGridView.IsNewItemRow(e.RowHandle);
                    break;
                case VisualStyleEnum.AdvBandedGridView:
                    IsNewItemRow = this.BaseAdvBandedGridView.IsNewItemRow(e.RowHandle);
                    break;
            }

            if (IsNewItemRow == false && e.Column.Tag is Views.Grid.Column column)
            {
                var eventArgs = new Views.Grid.CellValueChangingEventArgs(this, e.RowHandle, column, e.Value);
                CellValueChanged?.Invoke(this, eventArgs);
            }
        }

        private void GridView1_CustomRowCellEditForEditing(object sender, CustomRowCellEditEventArgs e)
        {
            Views.Grid.Column dc = (Views.Grid.Column)e.Column.Tag;
            switch (dc.ColumnEdit)
            {
                case ColumnEditEnum.Button:
                    e.RepositoryItem = this.buttonEdit1;
                    break;
                case ColumnEditEnum.ComboList:
                    if (dc.GridColumn.ColumnEdit is DevExpress.XtraEditors.Repository.RepositoryItemComboBox combobox)
                    {
                        combobox.Items.Clear();
                        combobox.Items.AddRange(dc.Items.ToArray());
                    }
                    break;
                case ColumnEditEnum.Color:
                    e.RepositoryItem = this.ColorEdit1;
                    break;
            }

            var info = new Views.Grid.CustomRowCellEditEventArgs(this, e);
            CustomRowCellEditForEditing?.Invoke(this, info);
        }

        private void GridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            var info = new Views.Grid.CustomRowCellEditEventArgs(this, e);
            CustomRowCellEdit?.Invoke(this, info);
        }

        private void BaseGridView_HiddenEditor(object sender, EventArgs e)
        {
            HiddenEditor?.Invoke(this, e);
        }

        private void ButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraGrid.Columns.GridColumn column = null;
            object Row = null;
            var rowHandle = default(int);
            switch (this.VisualStyle)
            {
                case VisualStyleEnum.GridView:
                    column = this.BaseGridView.FocusedColumn;
                    rowHandle = this.BaseGridView.FocusedRowHandle;
                    Row = this.BaseGridView.GetRow(rowHandle);
                    break;
                case VisualStyleEnum.AdvBandedGridView:
                    column = this.BaseAdvBandedGridView.FocusedColumn;
                    rowHandle = this.BaseAdvBandedGridView.FocusedRowHandle;
                    Row = this.BaseAdvBandedGridView.GetRow(rowHandle);
                    break;
            }

            // 触发相应事件==================================================
            if (column is object && column.Tag is Views.Grid.Column dc)
            {
                var eventArgs = new Views.Grid.RowColEventArgs(this, rowHandle, dc);
                CellButtonClick?.Invoke(this, eventArgs);
            }
        }



        // Private Sub GridView1_FocusedColumnChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs) Handles GridView1.FocusedColumnChanged

        // '触发相应事件==================================================
        // If e.FocusedColumn IsNot Nothing AndAlso TypeOf e.FocusedColumn.Tag Is Column Then
        // Dim dc As Column = e.FocusedColumn.Tag
        // Select Case dc.ColumnEdit
        // Case ColumnEditEnum.Button
        // e.FocusedColumn.ColumnEdit = Me.ButtonEdit1
        // Case ColumnEditEnum.ComboList
        // e.FocusedColumn.ColumnEdit = Me.ComboBoxEdit1
        // Case ColumnEditEnum.Color
        // e.FocusedColumn.ColumnEdit = Me.ColorEdit1
        // End Select
        // End If
        // End Sub

        /// <summary>
        /// 选择不同数据行后执行，也就是Current属性发生变化后执行。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (this.BindingSource.Position != this.OldPosition)
            {
                var args = new Views.Grid.GridEventArgs(this);
                CurrentChanged?.Invoke(this, args);
            }

            this.OldPosition = this.BindingSource.Position;
        }
        private int OldPosition = -1;

        /// <summary>
        /// 选择不同数据行后执行，也就是Current属性发生变化后执行。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void BindingSource_PositionChanged(object sender, EventArgs e)
        {
            var Args = new Views.Grid.GridEventArgs(this);
            PositionChanged?.Invoke(this, Args);
        }

        private void GridView1_CalcRowHeight(object sender, RowHeightEventArgs e)
        {
            if (sender is DevExpress.XtraGrid.Views.Base.BaseView)
            {
                var Row = ((DevExpress.XtraGrid.Views.Base.BaseView)sender).GetRow(e.RowHandle);
                if (Row is object)
                {
                    var Args = new Views.Grid.RowHeightEventArgs(e);
                    Args._GridControl = this;
                    Args._Row = Row;
                    CalcRowHeight?.Invoke(this, Args);
                }
            }
        }

        private void GridView1_GotFocus(object sender, EventArgs e)
        {
            GotFocus?.Invoke(this, e);
        }

        private void GridView1_LostFocus(object sender, EventArgs e)
        {
            LostFocus?.Invoke(this, e);
        }

        private void GridView1_CustomColumnGroup(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
        {
            var info = new Views.Grid.CustomColumnSortEventArgs(this, e);
            CustomColumnGroup?.Invoke(this, info);
        }

        private void GridView1_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
        {
            var info = new Views.Grid.CustomColumnSortEventArgs(this, e);
            CustomColumnSort?.Invoke(this, info);
        }

        private void GridView1_EndGrouping(object sender, EventArgs e)
        {
            EndGrouping?.Invoke(this, e);
        }

        private void GridView1_EndSorting(object sender, EventArgs e)
        {
            EndSorting?.Invoke(this, e);
        }

        private void GridView1_StartGrouping(object sender, EventArgs e)
        {
            StartGrouping?.Invoke(this, e);
        }

        private void GridView1_StartSorting(object sender, EventArgs e)
        {
            StartSorting?.Invoke(this, e);
        }

        private void GridView1_ColumnWidthChanged(object sender, DevExpress.XtraGrid.Views.Base.ColumnEventArgs e)
        {
            if (e.Column.Tag is Views.Grid.Column)
            {
                var info = new Views.Grid.ColumnEventArgs(this, e);
                ColumnWidthChanged?.Invoke(this, info);
            }
        }

        private void GridView1_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            if (!this.DesignMode)
            {
                if (e.FocusedColumn is object && e.FocusedColumn.Tag is Views.Grid.Column)
                {
                    var info = new Views.Grid.FocusedColumnChangedEventArgs(this, e);
                    FocusedColumnChanged?.Invoke(this, info);
                }
            }
        }

        private void GridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var info = new Views.Grid.FocusedRowChangedEventArgs((DevExpress.XtraGrid.Views.Base.BaseView)sender, e);
            info._GridControl = this;
            FocusedRowChanged?.Invoke(this, info);
        }

        private void GridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            var info = new Views.Grid.SelectionChangedEventArgs(e);
            SelectionChanged?.Invoke(this, info);
        }

        private void GridView1_TopRowChanged(object sender, EventArgs e)
        {
            TopRowChanged?.Invoke(this, e);
        }

        private void GridView1_MouseEnter(object sender, EventArgs e)
        {
            MouseEnter?.Invoke(this, e);
        }

        private void GridView1_MouseLeave(object sender, EventArgs e)
        {
            MouseLeave?.Invoke(this, e);
        }

        private void GridView1_MouseMove(object sender, MouseEventArgs e)
        {
            MouseMove?.Invoke(this, e);
            bool hand = false;
            GridHitInfo info = null;
            if (ReferenceEquals(sender, this.BaseGridView))
                info = this.BaseGridView.CalcHitInfo(e.X, e.Y);
            else if (ReferenceEquals(sender, this.BaseBandedGridView))
                info = this.BaseBandedGridView.CalcHitInfo(e.X, e.Y);
            else if (ReferenceEquals(sender, this.BaseAdvBandedGridView))
                info = this.BaseAdvBandedGridView.CalcHitInfo(e.X, e.Y);
            if (this.ContainsNewItemRow(info, e.Location) == true)
                hand = true;
            else if (info.Column is object && info.Column.Tag is Views.Grid.Column column)
            {
                switch (column.ColumnEdit)
                {
                    case ColumnEditEnum.Link:
                        if (this.ContainsLink(info, e.Location))
                            hand = true;
                        break;
                }
            }
            if (hand)
                this.GridControl1.Cursor = Cursors.Hand;
            else
                this.GridControl1.Cursor = Cursors.Default;
        }

        private void GridView1_MouseUp(object sender, MouseEventArgs e)
        {
            MouseUp?.Invoke(this, e);
        }

        private void GridView1_MouseWheel(object sender, MouseEventArgs e)
        {
            MouseWheel?.Invoke(this, e);
        }

        private void GridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Tag is Views.Grid.Column)
            {
                var info = new Views.Grid.CustomColumnDisplayTextEventArgs(e);
                info._GridControl = this;
                CustomColumnDisplayText?.Invoke(this, info);
            }
        }

        private void GridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            var info = new Views.Grid.RowCellStyleEventArgs(this, e);
            RowCellStyle?.Invoke(this, info);
        }

        private void GridView1_RowStyle(object sender, RowStyleEventArgs e)
        {
            var info = new Views.Grid.RowStyleEventArgs(this, e);
            RowStyle?.Invoke(this, info);
        }

        private void GridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column is not null && e.Column.Tag is object)
            {
                var info = new Views.Grid.ColumnHeaderCustomDrawEventArgs(e);
                info._GridControl = this;
                // 绘制列的锁定标志。
                if (this.Editable && !info.Column.AllowEdit)
                {
                    var icon = Properties.Resources.Lock;
                    int interval = (int)Math.Round((e.Bounds.Height - icon.Height) / 2d);
                    e.DefaultDraw();
                    e.Graphics.DrawIcon(icon, e.Bounds.Right - icon.Width - interval, e.Bounds.Y + interval);
                    e.Handled = true;
                }
                // 触发列标题的绘制事件。
                CustomDrawColumnHeader?.Invoke(this, info);
            }
        }

        private void GridView1_BeforeLeaveRow(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e)
        {
            var info = new Views.Grid.RowAllowEventArgs(e);
            info._GridControl = this;
            BeforeLeaveRow?.Invoke(this, info);
        }

        private void GridView1_Click(object sender, EventArgs e)
        {
            Click?.Invoke(this, e);
        }

        private void GridView1_DoubleClick(object sender, EventArgs e)
        {
            DoubleClick?.Invoke(this, e);
            // Dim FocusedRowHandle As Integer = -1
            // Dim Column As DevExpress.XtraGrid.Columns.GridColumn = Nothing
            // Dim Row As Object = Nothing
            // Dim IsNewItemRow As Boolean = False

            // If sender Is BaseGridView Then
            // Column = Me.BaseGridView.FocusedColumn
            // FocusedRowHandle = Me.BaseGridView.FocusedRowHandle
            // Row = Me.BaseGridView.GetRow(Me.BaseGridView.FocusedRowHandle)
            // IsNewItemRow = Me.BaseGridView.IsNewItemRow(Me.BaseGridView.FocusedRowHandle)
            // ElseIf sender Is BaseBandedGridView Then
            // Column = Me.BaseBandedGridView.FocusedColumn
            // FocusedRowHandle = Me.BaseBandedGridView.FocusedRowHandle
            // Row = Me.BaseBandedGridView.GetRow(Me.BaseBandedGridView.FocusedRowHandle)
            // IsNewItemRow = Me.BaseGridView.IsNewItemRow(Me.BaseGridView.FocusedRowHandle)
            // ElseIf sender Is BaseAdvBandedGridView Then
            // Column = Me.BaseAdvBandedGridView.FocusedColumn
            // FocusedRowHandle = Me.BaseAdvBandedGridView.FocusedRowHandle
            // Row = Me.BaseAdvBandedGridView.GetRow(Me.BaseAdvBandedGridView.FocusedRowHandle)
            // IsNewItemRow = Me.BaseAdvBandedGridView.IsNewItemRow(Me.BaseAdvBandedGridView.FocusedRowHandle)
            // End If
            // If IsNewItemRow = False AndAlso FocusedRowHandle >= 0 AndAlso Column IsNot Nothing AndAlso Row IsNot Nothing Then
            // Dim dc As Marmat.Forms.Views.Grid.Column = Column.Tag
            // Dim EventArgs As New  Marmat.Forms.Views.Grid.RowColEventArgs(Me, Row, dc)
            // RaiseEvent RowCellDoubleClick(Me, EventArgs)
            // End If
        }

        private void GridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            var info = new Views.Grid.InvalidRowExceptionEventArgs(e);
            info._GridControl = this;
            InvalidRowException?.Invoke(this, info);
        }

        private void GridView1_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            var info = new Views.Grid.InvalidValueExceptionEventArgs(e);
            info._GridControl = this;
            InvalidValueException?.Invoke(this, info);
        }

        private void GridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Column is object && e.Column.Tag is Views.Grid.Column column)
                {
                    // 逻辑列进入直接编辑
                    if (ReferenceEquals(e.Column.ColumnType, typeof(bool)) && e.Column.ReadOnly == false && this.Editable == true)
                    {
                        GridViewInfo ViewInfo = (GridViewInfo)((DevExpress.XtraGrid.Views.Base.BaseView)sender).GetViewInfo();
                        var CellInfo = ViewInfo.GetGridCellInfo(e.RowHandle, e.Column);
                        int x;
                        int y;
                        {
                            var withBlock = CellInfo.CellValueRect;
                            x = (int)Math.Round(withBlock.X + (withBlock.Width - 14) / 2d);
                            y = (int)Math.Round(withBlock.Y + (withBlock.Height - 14) / 2d);
                        }

                        var rect = new Rectangle(x, y, 14, 14);
                        if (rect.Contains(e.Location))
                        {
                            var info = new Views.Grid.RowColCancelEventArgs(this, e.RowHandle, column);
                            ShowingEditor?.Invoke(this, info);
                            if (info.Cancel == false)
                            {
                                bool cellValue = false;
                                if (!ReferenceEquals(e.CellValue, DBNull.Value))
                                {
                                    cellValue = Convert.ToBoolean(e.CellValue);
                                }

                                switch (this.VisualStyle)
                                {
                                    case VisualStyleEnum.GridView:
                                        this.BaseGridView.SetRowCellValue(e.RowHandle, e.Column, !cellValue);
                                        break;
                                    case VisualStyleEnum.AdvBandedGridView:
                                        this.BaseAdvBandedGridView.SetRowCellValue(e.RowHandle, e.Column, !cellValue);
                                        break;
                                }
                            }
                        }
                    }

                    if (e.RowHandle >= 0)
                    {
                        var EventArgs = new Views.Grid.MouseRowColEventArgs(this, e.RowHandle, column, e.Location, e.Button);
                        RowCellClick?.Invoke(this, EventArgs);
                    }
                }
            }
        }

        private void GridView1_RowClick(object sender, RowClickEventArgs e)
        {
            var info = new Views.Grid.RowClickEventArgs(e);
            RowClick?.Invoke(this, info);
        }

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            var info = new Views.Grid.ValidateRowEventArgs(e);
            ValidateRow?.Invoke(this, info);
        }

        private Dictionary<IBindingFieldNotification, string> _bindingFieldNotificationCollection;

        public void Subscription(string channel, IBindingFieldNotification field)
        {
            if (this._bindingFieldNotificationCollection is null)
                this._bindingFieldNotificationCollection = new Dictionary<IBindingFieldNotification, string>();
            if (!this._bindingFieldNotificationCollection.ContainsKey(field))
                this._bindingFieldNotificationCollection.Add(field, channel);
        }

        public void Unsubscribe(IBindingFieldNotification field)
        {
            if (this._bindingFieldNotificationCollection is object && this._bindingFieldNotificationCollection.ContainsKey(field))
                this._bindingFieldNotificationCollection.Remove(field);
        }

        internal void NotifyPropertyChanged()
        {
            if (this._bindingFieldNotificationCollection is not null)
            {
                foreach (var item in this._bindingFieldNotificationCollection)
                    item.Key.NotifyPropertyChanged();
            }
        }

        public virtual void BeginInit()
        {
        }

        public virtual void EndInit()
        {
            this.NotifyPropertyChanged();
        }

        private void BaseGridView_RowCountChanged(object sender, EventArgs e)
        {
            RowCountChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}